/*
 * Copyright 2011 Clean GWT
 *
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
 * use this file except in compliance with the License. You may obtain a copy of
 * the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 * License for the specific language governing permissions and limitations under
 * the License.
 */
package com.cleanform.gwt.chinesechess.client.widgets;


import com.cleanform.gwt.bootstrap.client.ui.Button;
import com.cleanform.gwt.bootstrap.client.ui.HeadingPanel;
import com.cleanform.gwt.bootstrap.client.ui.TextBox;
import com.cleanform.gwt.user.client.ActionEvent;
import com.cleanform.gwt.user.client.ActionEvent.ActionHandler;
import com.cleanform.gwt.user.client.ActionEvent.HasActionHandlers;
import com.google.gwt.dom.client.Style.Unit;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.event.dom.client.KeyDownEvent;
import com.google.gwt.event.dom.client.KeyDownHandler;
import com.google.gwt.event.shared.HandlerRegistration;
import com.google.gwt.user.client.ui.HTML;
import com.google.gwt.user.client.ui.ScrollPanel;

import static com.cleanform.gwt.user.client.ui.Layouts.hor;
import static com.cleanform.gwt.user.client.ui.Layouts.ver;

/**
 * The message pane for sending and displaying messages.
 * 
 * @author Jess
 * @version 1.0
 * @since 1.0
 */
public class MessagePane extends HeadingPanel implements HasActionHandlers {

	private ScrollPanel scroller = new ScrollPanel();
	private HTML txtDialog = new HTML();
	private TextBox txtMsgInput = new TextBox();
	private Button btnSend = new Button("Send");

	public MessagePane() {
		super(PanelType.PRIMARY);
		setCaption("Messages");
		setContentWidget(ver(scroller,hor(txtMsgInput , btnSend)));
		setHeight("300px");
		getElement().getStyle().setMargin(0, Unit.PX);
		
		scroller.setWidget(txtDialog);
		scroller.setHeight("186px");
		scroller.setWidth("100%");

		btnSend.addClickHandler(new ClickHandler() {

			@Override
			public void onClick(ClickEvent event) {
				sendMessage();
			}
		});

		txtMsgInput.addKeyDownHandler(new KeyDownHandler() {

			@Override
			public void onKeyDown(KeyDownEvent event) {
				if (event.getNativeKeyCode() == 13) {
					sendMessage();
				}
			}
		});
	}

	@Override
	public HandlerRegistration addActionHandler(ActionHandler handler) {
		return this.addHandler(handler, ActionEvent.getType());
	}

	public void appendMessage(String msg) {
		txtDialog.setHTML(txtDialog.getHTML() + msg + "<br/>");
	}

	public void appendMessage(String player, String msg) {
		appendMessage(player + " : " + msg);
	}

	public void appendSysMessage(String msg) {
		this.appendMessage("<font color='gray'>" + msg + "</font>");
	}

	/**
	 * @return
	 */
	public String getInputMessage() {
		return txtMsgInput.getValue();
	}

	/**
	 * Clears messages.
	 */
	public void reset() {
		txtDialog.setText("");
		txtMsgInput.setText("");
	}

	private void sendMessage() {
		ActionEvent.fire(MessagePane.this, "sendMessage");
		txtMsgInput.setText("");
	}
}
